/* eslint-disable */
"use strict";

/**
 * @deprecated use-less, use npm version
 */
// @dotenv-run/rspack
/**
 * @import { Compiler, RspackPluginInstance } from "@rspack/core";
 * @import { Dict, DotenvRunOptions } from "@dotenv-run/core";
 */
const { env } = require("@dotenv-run/core");
const { DefinePlugin } = require("@rspack/core");

/**
 * @implements {RspackPluginInstance}
 */
class DotenvRunPlugin {
  /**
   * @type {Dict}
   */
  raw = {};

  /**
   * @type {Dict}
   */
  full = {};

  /**
   * @type {Dict}
   */
  stringified = {};

  ssr;

  /**
   * @param {DotenvRunOptions} options
   * @param {boolean} ssr
   */
  constructor(options, ssr = false) {
    const { full, stringified, raw } = env(options);
    this.raw = raw;
    this.full = full;
    this.stringified = stringified;
    this.ssr = ssr;
  }

  /**
   * @param {Compiler} compiler
   */
  apply(compiler) {
    const definePlugin = new DefinePlugin(
      this.ssr
        ? { ...this.full }
        : {
            "process.env": this.stringified,
            "import.meta.env": this.stringified,
            "import.meta.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
          },
    );
    definePlugin.apply(compiler);
  }
}

module.exports = { DotenvRunPlugin };
